#include "fenoption.h"
#include "ui_fenoption.h"

FenOption::FenOption(ListeProf listeProf, GuniOption options, QStringList listeInstru, QWidget *parent) :
    QDialog(parent)
{
    setupUi(this);

    receptionListeProf(listeProf);
    receptionListeInstru(listeInstru);
    receptionOptions(options);

    QObject::connect(pushButton, SIGNAL(clicked()), this, SLOT(AjouterInstru()));
    QObject::connect(this, SIGNAL(accepted()), this, SLOT(BtnOK()));
}

FenOption::~FenOption()
{
    delete this;
}
void FenOption::receptionListeInstru(QStringList linstru)
{
    if (linstru.isEmpty())
    {
        linstru << "Guitare";
    }
    modele = new QStringListModel(linstru);
    instrusListView->setModel(modele);
}
void FenOption::receptionListeProf(ListeProf lpf)
{

    maListeProf = lpf;

    QStringList sListeProf;
    for (int i = 0; i < lpf.count(); i++)
    {
        sListeProf << lpf.at(i).getInfo().NomEtPrenom();
    }
    profParDFautComboBox->addItems(sListeProf);
}

void FenOption::receptionOptions(GuniOption guniOption)
{
    mesOptions = guniOption;
    afficherTousLesLVeSCheckBox->setChecked(mesOptions.getAfficherTousEleves());
    connexionAutomatiqueCheckBox->setChecked(mesOptions.getConnectionAuto());
    montantParDFautDeLaCotisationSpinBox->setValue(mesOptions.getMontantParDefautCotisation());

    int monIndexCombo = 0;

    for (int i = 0; i < profParDFautComboBox->count(); i++)
    {
        if (profParDFautComboBox->itemText(i) == mesOptions.getProfParDefaut().getInfo().NomEtPrenom())
            monIndexCombo = i;

    }

    profParDFautComboBox->setCurrentIndex(monIndexCombo);
}
void FenOption::AjouterInstru()
{
    if (!instruLineEdit->text().isEmpty())
    {
        QStringList li = modele->stringList();
        if (!li.contains(instruLineEdit->text(), Qt::CaseInsensitive) || li.isEmpty())
        {
            li << instruLineEdit->text();
            modele->setStringList(li);
            instruLineEdit->setText("");
        }
        else
        {
            QMessageBox::information(NULL, INFO, CETINSTRUMENTEXISTEDEJA);
        }
    }
}
void FenOption::BtnOK()
{
    mesOptions.setAfficherTousEleves(afficherTousLesLVeSCheckBox->isChecked());
    mesOptions.setConnectionAuto(connexionAutomatiqueCheckBox->isChecked());
    mesOptions.setMontantParDefautCotisation(montantParDFautDeLaCotisationSpinBox->value());
    mesOptions.setProfParDefaut(maListeProf.getProf(profParDFautComboBox->currentText()));

    emit TransmetOptions(mesOptions);
    emit TransmetListeInstru(modele->stringList());
    this->close();
}
